<script setup lang="ts">
import { ref } from "vue";
import { workOrderConfig } from "./utils/hook";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { usePublicHooks } from "../hooks";
import { usePublicFormats } from "../formats";
import { PureTableBar } from "@/components/RePureTableBar";

import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
import EditPen from "@iconify-icons/ep/edit-pen";

defineOptions({
  name: "workOrder"
});

const formRef = ref();
const { timeFrame } = usePublicHooks();
const { orderStatusOptions, orderTypeOptions } = usePublicFormats();

const {
  roles,
  columns,
  form,
  loading,
  onSearch,
  resetForm,
  dataList,
  pagination,
  onSizeChange,
  onCurrentChange,
  loadingConfig,
  openDialog,
  openDialogReply
} = workOrderConfig();
</script>

<template>
  <div>
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
      :header-cell-style="{
        background: 'var(--el-fill-color-light)',
        color: 'var(--el-text-color-primary)'
      }"
    >
      <el-form-item label="停车场名称" prop="parking_lot_name" class="!w-[20%]">
        <el-input
          v-model="form.parking_lot_name"
          placeholder="请输入停车场名称"
          clearable
          class="!w-[160px]"
        />
      </el-form-item>

      <el-form-item label="车牌号" prop="car_number">
        <el-input
          v-model="form.car_number"
          clearable
          placeholder="请输入车牌号"
        />
      </el-form-item>

      <el-form-item label="状态" prop="reconciliation_status" class="!w-[20%]">
        <el-select v-model="form.status" placeholder="请选择状态" clearable>
          <el-option label="全部" :value="-1" />
          <el-option label="待处理" :value="0" />
          <el-option label="已处理" :value="1" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar title="工单管理" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <el-button
          v-if="!'admin'.includes(roles)"
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog()"
        >
          新增
        </el-button>
      </template>
      <pure-table
        v-if="dataList.length > 0"
        ref="tableRef"
        border
        stripe
        row-key="id"
        alignWhole="center"
        showOverflowTooltip
        :loading="loading"
        :loading-config="loadingConfig"
        :columns="columns"
        :data="dataList"
        :header-cell-style="{
          background: 'var(--el-fill-color-light)',
          color: 'var(--el-text-color-primary)'
        }"
        :pagination="pagination"
        @page-size-change="onSizeChange"
        @page-current-change="onCurrentChange"
      >
        <template #image="{ row, index }">
          <div v-for="(photoUrl, photoIndex) in row.image" :key="photoIndex">
            <el-image
              preview-teleported
              loading="lazy"
              :src="photoUrl"
              :initial-index="index"
              :preview-src-list="dataList.map(v => photoUrl)"
              fit="cover"
              class="h-[50px] images"
            />
          </div>
        </template>
        <template #operation="{ row }">
          <el-button
            v-if="!'admin'.includes(roles)"
            class="reset-margin"
            link
            type="primary"
            :icon="useRenderIcon(EditPen)"
            @click="openDialog('修改', row)"
          >
            修改
          </el-button>

          <el-button
            v-if="'admin'.includes(roles)"
            class="reset-margin"
            link
            type="primary"
            @click="openDialogReply('回复', row)"
          >
            回复
          </el-button>
        </template>
      </pure-table>
    </PureTableBar>
  </div>
</template>
<style lang="scss" scoped></style>
